package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;

/**
 * @Author liking
 * @Description
 * @Date 2025/4/28
 * @Version 1.0
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;

    @ApiOperation("获取所有医院设置")
    @GetMapping("findAll")
    public Result<List<HospitalSet>> getAllData(){
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation("删除医院设置")
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable("id") Long id){
        boolean flag = hospitalSetService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
//        int i = 100/0;
        boolean flag = hospitalSetService.removeByIds(idList);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation("获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result get(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    @ApiOperation("更新医院设置")
    @PostMapping("updateHospitalSet")
    public Result update(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation("医院锁定或者解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result save(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        HospitalSet byId = hospitalSetService.getById(id);
        byId.setStatus(status);
        boolean flag = hospitalSetService.updateById(byId);
        return flag ? Result.ok() : Result.fail();
    }

    @ApiOperation("添加医院设置")
    @PostMapping("saveHospitalSet")
    public Result save(@RequestBody HospitalSet hospitalSet){

        Random random = new Random();
        //设置状态 1 使用 0 不能使用
        hospitalSet.setStatus(1);
        //签名秘钥
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);
        return save ? Result.ok() : Result.fail();
    }

    @ApiOperation("分页查询所有医院设置")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable("current") Long current,
                                  @PathVariable("limit") Long limit,
                                  @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        if(Objects.isNull(hospitalSetQueryVo)){
            hospitalSetQueryVo = new HospitalSetQueryVo();
        }
        //创建page对象
        Page<HospitalSet> page = new Page<>(current,limit);
        //调用方法实现分页
        Page<HospitalSet> pateList = hospitalSetService.page(page, new LambdaQueryWrapper<HospitalSet>()
                .eq(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode()), HospitalSet::getHoscode, hospitalSetQueryVo.getHoscode())
                .like(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname()), HospitalSet::getHosname, hospitalSetQueryVo.getHosname())
                .orderByDesc(HospitalSet::getCreateTime));

        return Result.ok(pateList);

    }

}
